home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Frameworks
/
Sprocket Framework DR2
/
Sprocket Framework
/
DynamicArray.cp
< prev
next >
Wrap
Text File
|
1996-06-15
|
4KB
|
204 lines
/*
File: DynamicArray.cp
Project: Sprocket Framework 1.1 (DR2), released 6/15/96
Contains: Array class which adjusts size dynamically
To Do: ?
Sprocket Major Contributors:
----------------------------
Dave Falkenburg, producer of Sprocket 1.0
Bill Hayden, producer of Sprocket 1.1
Steve Sisak, producer of the upcoming Sprocket 2.0
Pete Alexander Steve Falkenburg Randy Thelen
Eric Berdahl Nitin Ganatra Chris K. Thomas
Marshall Clow Dave Hershey Leonard Rosenthal
Tim Craycroft Dave Mark Dean Yu
David denBoer Gary Powell
Cameron Esfahani Jon Summers Apple Computer, Inc.
Comments, Additions, or Corrections:
------------------------------------
Bill Hayden, Nikol Software <nikol@codewell.com>
*/
#include "DynamicArray.h"
#include <Memory.h>
#include <Errors.h>
TDynamicArray::TDynamicArray()
{
fElementCount = 0;
fStorage = (ArrayElementPtr **) NewHandle(0);
}
TDynamicArray::~TDynamicArray()
{
DisposeHandle((Handle) fStorage);
}
OSErr
TDynamicArray::Insert(ArrayElement * elementToInsert, ArrayElementIndex beforeElement)
{
OSErr err;
// make room for one more ArrayElementPtr
SetHandleSize((Handle) fStorage, fElementCount*sizeof(ArrayElementPtr) + sizeof(ArrayElementPtr));
if ((err = MemError()) != noErr)
return err;
// slide remaining elements down
BlockMoveData( &((*fStorage)[beforeElement]),
&((*fStorage)[beforeElement+1]),
(fElementCount - beforeElement) * sizeof(ArrayElementPtr));
fElementCount++;
(*fStorage)[beforeElement] = elementToInsert;
return noErr;
}
OSErr
TDynamicArray::InsertFirst(ArrayElement * elementToInsert)
{
return this->Insert(elementToInsert, 0);
}
OSErr
TDynamicArray::InsertLast(ArrayElement * elementToInsert)
{
return this->Insert(elementToInsert, fElementCount);
}
OSErr TDynamicArray::Delete(ArrayElementIndex whichElement)
{
// slide remaining elements up
BlockMoveData( &((*fStorage)[whichElement+1]),
&((*fStorage)[whichElement]),
(fElementCount - whichElement) * sizeof(ArrayElementPtr));
// cut back the storage
fElementCount--;
SetHandleSize((Handle) fStorage,fElementCount*sizeof(ArrayElementPtr));
return MemError();
}
OSErr TDynamicArray::DeleteFirst()
{
return this->Delete(0);
}
OSErr TDynamicArray::DeleteLast()
{
return this->Delete(fElementCount);
}
ArrayElementPtr TDynamicArray::GetIndexedElement(ArrayElementIndex whichElement)
{
return (*fStorage)[whichElement];
}
void TDynamicArray::SetIndexedElement(ArrayElementIndex whichElement, ArrayElementPtr element)
{
(*fStorage)[whichElement] = element;
}
void TDynamicArray::ForEachElement(EachArrayElementProc proc, void * param)
{
ArrayElementIndex index = 0;
while (index < fElementCount)
{
(*proc)((*fStorage)[index], param);
index++;
}
}
ArrayElementPtr TDynamicArray::FirstElementThat(EachArrayElementTestProc proc, void * param)
{
ArrayElementIndex index = 0;
while (index < fElementCount)
{
if ((*proc)((*fStorage)[index], param))
return (*fStorage)[index];
index++;
}
return NULL;
}
ArrayElementPtr TDynamicArray::LastElementThat(EachArrayElementTestProc proc, void * param)
{
ArrayElementIndex index = fElementCount-1;
while (index != -1)
{
if ((*proc)((*fStorage)[index], param))
return (*fStorage)[index];
index--;
}
return NULL;
}
OSErr TDynamicArray::FindAndDeleteElement(ArrayElementPtr element)
{
ArrayElementIndex index = 0;
OSErr err = userDataItemNotFound;
while (index < fElementCount)
{
if ( (*fStorage)[index] == element )
{
err = this->Delete(index);
break;
}
index++;
}
return err;
}
OSErr TDynamicArray::MoveToFront(ArrayElementPtr elementToMove)
{
ArrayElementIndex index;
for (index = 0; index < fElementCount; index++)
{
if (elementToMove == (*fStorage)[index])
{
if (index != 0)
{
BlockMoveData(&(*fStorage)[0], &(*fStorage)[1], index * sizeof(ArrayElementPtr));
(*fStorage)[0] = elementToMove;
}
return noErr;
}
}
return userDataItemNotFound;
}